package com.sdry.service.impl.cb;

import com.sdry.mapper.cb.StockMapper;
import com.sdry.model.lz.Materiel;
import com.sdry.model.zc.ZcInventoryInfoEntity;
import com.sdry.service.cb.StockService;
import com.sdry.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * (propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
 */
@Service
public class StockServiceImpl implements StockService {
    @Autowired
    private StockMapper stockMapper;

    @Override
    public List<ZcInventoryInfoEntity> getStockList(Long customerId, String maNum) {
        if (StringUtil.isEmpty(maNum)){
            maNum = null;
        }
        if ("".equals(customerId)) {
            customerId = null;
        }
        return stockMapper.getStockList(customerId,maNum);
    }

    @Override
    public List<Materiel> getTiaoMaList(Integer page, Integer pageSize, String mid) {


        return stockMapper.getTiaoMaList((page - 1) * pageSize,pageSize,mid);
    }

    @Override
    public Integer getTiaoMaTotal(String mid) {
        return stockMapper.getTiaoMaTotal(mid);
    }

    @Override
    public void insert() {
        List<Materiel> list = stockMapper.selectMateriel();
        Long a = 10033L;
        for (Materiel ma : list) {
            ma.setCustomer_id(ma.getCustomer_id()+a);
            stockMapper.insert(ma);
            ma = null;
        }
    }

    @Override
    public void testTransactional (){
        for (int i = 0; i < 10000; i++) {
            Integer id = stockMapper.save01("陈彬01");
            if (i == 9999) {
               int b = 1/0;
            }
            stockMapper.save02(id.toString());
        }
        /*try {
            int a = 0;
            stockMapper.save01("陈彬01");
            //int b = a/0;
            stockMapper.save02("陈彬02");
        } catch (Exception e) {
            //手动开启事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }*/
    }

}
